package cn.breeze.system.service.impl;

import cn.breeze.org.domain.Department;
import cn.breeze.system.domain.Menu;
import cn.breeze.system.mapper.MenuMapper;
import cn.breeze.system.service.IMenuService;
import cn.breeze.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author breeze
 * @since 2022-07-11
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> loadMenuTree() {
        List<Menu> menuAll = menuMapper.loadAll();
        Map<Long, Menu> collect = menuAll.stream().collect(Collectors.toMap(Menu::getId, item -> item));
        List<Menu> menuList = new LinkedList<Menu>();
        for (Menu first : menuAll){
            if (first.getParentId() == null){
                menuList.add(first);
            } else {
                Menu menu = collect.get(first.getParentId());
                if (menu != null){
                    menu.getChildren().add(first);
                }
            }
        }
        return menuList;
    }
}
